package Gui;

import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

import DB.categoryConnector;

@SuppressWarnings("serial")
public class AddCategoryFrame extends JFrame {

	private JPanel contentPane;
	private JTextField urlTxt;
	private JTextField categoryTxt;
	private JTextField hintColTxt;
	private JTextField HintPrefixTxt;
	private int currentCol;

	/**
	 * Create the frame.
	 */
	public void clearFrame(){
		urlTxt.setText("");
		categoryTxt.setText("");
		HintPrefixTxt.setText("");
		hintColTxt.setText("");
	}
	
	public AddCategoryFrame() {
		setTitle("Wheel Of Fortune");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 548, 506);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JTextPane txtTitle = new JTextPane();
		txtTitle.setEditable(false);
		txtTitle.setFont(new Font("Tahoma", Font.BOLD, 15));
		txtTitle.setBackground(UIManager.getColor("Button.background"));
		txtTitle.setText("Add Category");
		txtTitle.setBounds(185, 22, 136, 20);
		contentPane.add(txtTitle);

		JButton btnOk = new JButton("Ok");
		btnOk.setFont(new Font("Tahoma", Font.PLAIN, 13));

		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				boolean canAdd = true;
				if (urlTxt.getText().isEmpty()){
					canAdd = false;
				}
				else if (categoryTxt.getText().isEmpty()){
					canAdd = false;
				}
				else if (HintPrefixTxt.getText().isEmpty()){
					canAdd = false;
				}
				else if (hintColTxt.getText().isEmpty()){
					canAdd = false;
				}
				else {
					String colum = hintColTxt.getText();
					try{
						currentCol = Integer.parseInt(colum);
						canAdd = currentCol > 1 ;
					}catch (NumberFormatException e) {
						canAdd = false;
					}
					
				}
				canAdd = urlTxt.getText().startsWith("http://download.freebase.com/datadumps/latest/browse/") && 
						urlTxt.getText().endsWith(".tsv");
				if (canAdd){
					String suc = categoryConnector.getInstance().addCategoryToDB(categoryTxt.getText(), urlTxt.getText(), currentCol, HintPrefixTxt.getText());
					JOptionPane.showMessageDialog(AddCategoryFrame.this, suc);
					setVisible(false);
					
				}
				else{
					JOptionPane.showMessageDialog(AddCategoryFrame.this, "Arguments are incorect or missing");
				}
				
			}});
		btnOk.setBounds(115, 405, 128, 52);
		contentPane.add(btnOk);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
			}});
		btnCancel.setFont(new Font("Tahoma", Font.PLAIN, 13));
		btnCancel.setBounds(285, 405, 128, 52);
		contentPane.add(btnCancel);
		
		JTextPane txtpnTableUrlPath = new JTextPane();
		txtpnTableUrlPath.setText("Table URL Path:");
		txtpnTableUrlPath.setFont(new Font("Tahoma", Font.PLAIN, 13));
		txtpnTableUrlPath.setEditable(false);
		txtpnTableUrlPath.setBackground(SystemColor.menu);
		txtpnTableUrlPath.setBounds(37, 86, 106, 20);
		contentPane.add(txtpnTableUrlPath);
		
		JTextPane txtpnThisToolIs = new JTextPane();
		txtpnThisToolIs.setText("This Tool is designed to help you add your own category from the freebase Database.\r\nImportent note: The freebase tables must have the name column first and the freebase ID second.");
		txtpnThisToolIs.setFont(new Font("Tahoma", Font.PLAIN, 9));
		txtpnThisToolIs.setEditable(false);
		txtpnThisToolIs.setBackground(SystemColor.menu);
		txtpnThisToolIs.setBounds(37, 54, 420, 38);
		contentPane.add(txtpnThisToolIs);
		
		JTextPane txtpnForExample = new JTextPane();
		txtpnForExample.setText("For example : http://download.freebase.com/datadumps/latest/browse/food/culinary_tool.tsv");
		txtpnForExample.setFont(new Font("Tahoma", Font.PLAIN, 9));
		txtpnForExample.setEditable(false);
		txtpnForExample.setBackground(SystemColor.menu);
		txtpnForExample.setBounds(37, 105, 420, 20);
		contentPane.add(txtpnForExample);
		
		urlTxt = new JTextField();
		urlTxt.setFont(new Font("Tahoma", Font.PLAIN, 9));
		urlTxt.setBounds(37, 126, 434, 20);
		contentPane.add(urlTxt);
		urlTxt.setColumns(10);
		
		JTextPane txtpnCategoryName = new JTextPane();
		txtpnCategoryName.setText("Category Name:");
		txtpnCategoryName.setFont(new Font("Tahoma", Font.PLAIN, 13));
		txtpnCategoryName.setEditable(false);
		txtpnCategoryName.setBackground(SystemColor.menu);
		txtpnCategoryName.setBounds(37, 153, 106, 20);
		contentPane.add(txtpnCategoryName);
		
		JTextPane txtpnForExample_1 = new JTextPane();
		txtpnForExample_1.setText("The category name that will be displayed as definition of the phrase.\r\nFor example : Culinary Tools");
		txtpnForExample_1.setFont(new Font("Tahoma", Font.PLAIN, 9));
		txtpnForExample_1.setEditable(false);
		txtpnForExample_1.setBackground(SystemColor.menu);
		txtpnForExample_1.setBounds(37, 172, 420, 25);
		contentPane.add(txtpnForExample_1);
		
		categoryTxt = new JTextField();
		categoryTxt.setFont(new Font("Tahoma", Font.PLAIN, 9));
		categoryTxt.setColumns(10);
		categoryTxt.setBounds(37, 208, 434, 20);
		contentPane.add(categoryTxt);
		
		JTextPane txtpnHintColumn = new JTextPane();
		txtpnHintColumn.setText("Hint Column:");
		txtpnHintColumn.setFont(new Font("Tahoma", Font.PLAIN, 13));
		txtpnHintColumn.setEditable(false);
		txtpnHintColumn.setBackground(SystemColor.menu);
		txtpnHintColumn.setBounds(37, 239, 106, 20);
		contentPane.add(txtpnHintColumn);
		
		JTextPane txtpnThecolumnWhereTo = new JTextPane();
		txtpnThecolumnWhereTo.setText("Thecolumn where to fing the hint. \r\nFor example : 2");
		txtpnThecolumnWhereTo.setFont(new Font("Tahoma", Font.PLAIN, 9));
		txtpnThecolumnWhereTo.setEditable(false);
		txtpnThecolumnWhereTo.setBackground(SystemColor.menu);
		txtpnThecolumnWhereTo.setBounds(37, 258, 420, 25);
		contentPane.add(txtpnThecolumnWhereTo);
		
		hintColTxt = new JTextField();
		hintColTxt.setFont(new Font("Tahoma", Font.PLAIN, 9));
		hintColTxt.setColumns(10);
		hintColTxt.setBounds(37, 294, 434, 20);
		contentPane.add(hintColTxt);
		
		JTextPane txtpnHintPrefix = new JTextPane();
		txtpnHintPrefix.setText("Hint Prefix:");
		txtpnHintPrefix.setFont(new Font("Tahoma", Font.PLAIN, 13));
		txtpnHintPrefix.setEditable(false);
		txtpnHintPrefix.setBackground(SystemColor.menu);
		txtpnHintPrefix.setBounds(37, 325, 106, 20);
		contentPane.add(txtpnHintPrefix);
		
		JTextPane txtpnThePrefixFor = new JTextPane();
		txtpnThePrefixFor.setText("The prefix for the hunt, usually the hint column name\r\nFor example : Recipts using this tools:");
		txtpnThePrefixFor.setFont(new Font("Tahoma", Font.PLAIN, 9));
		txtpnThePrefixFor.setEditable(false);
		txtpnThePrefixFor.setBackground(SystemColor.menu);
		txtpnThePrefixFor.setBounds(37, 344, 420, 25);
		contentPane.add(txtpnThePrefixFor);
		
		HintPrefixTxt = new JTextField();
		HintPrefixTxt.setFont(new Font("Tahoma", Font.PLAIN, 9));
		HintPrefixTxt.setColumns(10);
		HintPrefixTxt.setBounds(37, 380, 434, 20);
		contentPane.add(HintPrefixTxt);
	}
}
